#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
深度分析系统 - 第六步执行模块
对选中的Top 3股票进行深度分析，识别价格行为模式
"""

import sys
import os
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import warnings
warnings.filterwarnings('ignore')

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from real_data_fetcher import RealDataFetcher
from simple_technical_analysis import SimpleTechnicalAnalysis

class DeepAnalysisSystem:
    """深度分析系统"""
    
    def __init__(self):
        self.data_fetcher = RealDataFetcher()
        self.technical_analyzer = SimpleTechnicalAnalysis()
        
    def print_header(self, title):
        """打印标题"""
        print(f"\n{'='*80}")
        print(f"🔬 {title}")
        print(f"{'='*80}")
        
    def load_final_ranking(self, filename="final_investment_ranking.csv"):
        """加载最终排序结果"""
        try:
            df = pd.read_csv(filename)
            return df.to_dict('records')
        except Exception as e:
            print(f"加载最终排序结果失败: {e}")
            return []
            
    def get_extended_historical_data(self, stock_code, days=250):
        """获取扩展历史数据用于深度分析"""
        try:
            data = self.data_fetcher.get_stock_data(stock_code, days)
            if data is not None and not data.empty:
                return data
            return None
        except Exception as e:
            print(f"获取 {stock_code} 扩展历史数据失败: {e}")
            return None
            
    def analyze_price_patterns(self, stock_data):
        """分析价格行为模式"""
        try:
            patterns = {}
            
            # 计算价格统计特征
            close_prices = stock_data['close']
            returns = close_prices.pct_change().dropna()
            
            patterns['price_volatility'] = returns.std() * 100  # 波动率%
            patterns['max_drawdown'] = self.calculate_max_drawdown(close_prices)
            patterns['upward_days_ratio'] = (returns > 0).sum() / len(returns) * 100
            
            # 价格区间分析
            patterns['price_range_52w'] = {
                'high': close_prices.max(),
                'low': close_prices.min(),
                'current_position': ((close_prices.iloc[-1] - close_prices.min()) / 
                                   (close_prices.max() - close_prices.min()) * 100)
            }
            
            # 趋势持续性分析
            patterns['trend_strength'] = self.analyze_trend_strength(close_prices)
            
            # 成交量价格关系
            patterns['volume_price_correlation'] = self.analyze_volume_price_correlation(stock_data)
            
            return patterns
            
        except Exception as e:
            print(f"分析价格模式失败: {e}")
            return {}
            
    def calculate_max_drawdown(self, price_series):
        """计算最大回撤"""
        try:
            cumulative = (1 + price_series.pct_change()).cumprod()
            running_max = cumulative.expanding().max()
            drawdown = (cumulative - running_max) / running_max
            return abs(drawdown.min() * 100)  # 返回百分比
        except:
            return 0
            
    def analyze_trend_strength(self, price_series):
        """分析趋势强度"""
        try:
            # 计算不同周期的MA
            ma5 = price_series.rolling(5).mean()
            ma20 = price_series.rolling(20).mean()
            ma60 = price_series.rolling(60).mean()
            
            # 趋势一致性得分
            current_price = price_series.iloc[-1]
            trend_score = 0
            
            if current_price > ma5.iloc[-1]:
                trend_score += 1
            if ma5.iloc[-1] > ma20.iloc[-1]:
                trend_score += 1
            if ma20.iloc[-1] > ma60.iloc[-1]:
                trend_score += 1
                
            # MA斜率分析
            ma20_slope = (ma20.iloc[-1] - ma20.iloc[-10]) / ma20.iloc[-10] * 100
            
            return {
                'trend_consistency': trend_score / 3 * 100,  # 趋势一致性%
                'ma20_slope': ma20_slope  # MA20斜率%
            }
        except:
            return {'trend_consistency': 50, 'ma20_slope': 0}
            
    def analyze_volume_price_correlation(self, stock_data):
        """分析成交量价格关系"""
        try:
            price_changes = stock_data['close'].pct_change()
            volume = stock_data['volume']
            
            # 计算相关性
            correlation = price_changes.corr(volume)
            
            # 分析放量上涨/下跌比例
            up_days = price_changes > 0
            high_volume = volume > volume.median()
            
            volume_up_ratio = ((up_days & high_volume).sum() / up_days.sum() * 100 
                             if up_days.sum() > 0 else 0)
            
            return {
                'price_volume_correlation': correlation if not np.isnan(correlation) else 0,
                'volume_up_ratio': volume_up_ratio
            }
        except:
            return {'price_volume_correlation': 0, 'volume_up_ratio': 50}
            
    def identify_support_resistance_levels(self, stock_data):
        """识别关键支撑阻力位"""
        try:
            close_prices = stock_data['close']
            high_prices = stock_data['high']
            low_prices = stock_data['low']
            
            # 寻找局部高点和低点
            highs = []
            lows = []
            
            for i in range(5, len(close_prices) - 5):
                # 局部高点
                if (high_prices.iloc[i] > high_prices.iloc[i-5:i].max() and 
                    high_prices.iloc[i] > high_prices.iloc[i+1:i+6].max()):
                    highs.append(high_prices.iloc[i])
                    
                # 局部低点
                if (low_prices.iloc[i] < low_prices.iloc[i-5:i].min() and 
                    low_prices.iloc[i] < low_prices.iloc[i+1:i+6].min()):
                    lows.append(low_prices.iloc[i])
            
            # 统计重要价位
            current_price = close_prices.iloc[-1]
            
            # 阻力位：高于当前价的高点
            resistance_levels = [h for h in highs if h > current_price]
            resistance_levels.sort()
            
            # 支撑位：低于当前价的低点
            support_levels = [l for l in lows if l < current_price]
            support_levels.sort(reverse=True)
            
            return {
                'immediate_resistance': resistance_levels[:3] if resistance_levels else [],
                'immediate_support': support_levels[:3] if support_levels else [],
                'current_price': current_price
            }
            
        except Exception as e:
            print(f"识别支撑阻力位失败: {e}")
            return {'immediate_resistance': [], 'immediate_support': [], 'current_price': 0}
            
    def analyze_trading_behavior(self, stock_data):
        """分析交易行为特征"""
        try:
            close_prices = stock_data['close']
            volume = stock_data['volume']
            
            # 计算日内波动
            intraday_volatility = ((stock_data['high'] - stock_data['low']) / 
                                 stock_data['close'] * 100).mean()
            
            # 分析跳空情况
            gaps = []
            for i in range(1, len(stock_data)):
                prev_close = stock_data['close'].iloc[i-1]
                curr_open = stock_data['open'].iloc[i] if 'open' in stock_data.columns else stock_data['close'].iloc[i]
                gap_pct = (curr_open - prev_close) / prev_close * 100
                if abs(gap_pct) > 2:  # 跳空超过2%
                    gaps.append(gap_pct)
            
            # 计算换手率特征（模拟）
            avg_volume = volume.mean()
            volume_volatility = volume.std() / avg_volume * 100
            
            return {
                'intraday_volatility': intraday_volatility,
                'gap_frequency': len(gaps),
                'avg_gap_size': np.mean(gaps) if gaps else 0,
                'volume_volatility': volume_volatility,
                'avg_daily_volume': avg_volume
            }
            
        except Exception as e:
            print(f"分析交易行为失败: {e}")
            return {}
            
    def calculate_risk_metrics(self, stock_data):
        """计算风险指标"""
        try:
            close_prices = stock_data['close']
            returns = close_prices.pct_change().dropna()
            
            # 基本风险指标
            volatility = returns.std() * np.sqrt(252) * 100  # 年化波动率
            downside_volatility = returns[returns < 0].std() * np.sqrt(252) * 100
            
            # VaR计算（5%分位数）
            var_5 = np.percentile(returns, 5) * 100
            
            # 最大连续下跌天数
            consecutive_down = 0
            max_consecutive_down = 0
            for ret in returns:
                if ret < 0:
                    consecutive_down += 1
                    max_consecutive_down = max(max_consecutive_down, consecutive_down)
                else:
                    consecutive_down = 0
            
            # 夏普比率（假设无风险利率3%）
            excess_returns = returns.mean() * 252 - 0.03
            sharpe_ratio = excess_returns / (returns.std() * np.sqrt(252))
            
            return {
                'annual_volatility': volatility,
                'downside_volatility': downside_volatility,
                'var_5_percent': var_5,
                'max_consecutive_down_days': max_consecutive_down,
                'sharpe_ratio': sharpe_ratio
            }
            
        except Exception as e:
            print(f"计算风险指标失败: {e}")
            return {}
            
    def deep_analyze_single_stock(self, stock_info):
        """深度分析单只股票"""
        stock_code = stock_info['code']
        stock_name = stock_info['name']
        
        print(f"\n🔬 深度分析: {stock_code} {stock_name}")
        print(f"   📊 综合评分: {stock_info.get('comprehensive_score', 0):.1f}/100")
        print("-" * 70)
        
        # 获取扩展历史数据
        stock_data = self.get_extended_historical_data(stock_code, 250)
        if stock_data is None:
            print(f"   ❌ 无法获取扩展历史数据")
            return None
            
        print(f"   ✅ 获取到 {len(stock_data)} 天历史数据")
        
        # 价格行为模式分析
        print(f"\n   📈 价格行为模式分析:")
        price_patterns = self.analyze_price_patterns(stock_data)
        
        if price_patterns:
            print(f"      • 价格波动率: {price_patterns.get('price_volatility', 0):.1f}%")
            print(f"      • 最大回撤: {price_patterns.get('max_drawdown', 0):.1f}%")
            print(f"      • 上涨日占比: {price_patterns.get('upward_days_ratio', 0):.1f}%")
            
            price_pos = price_patterns.get('price_range_52w', {}).get('current_position', 0)
            print(f"      • 当前价格位置: {price_pos:.1f}% (在年度区间中)")
            
            trend_strength = price_patterns.get('trend_strength', {})
            print(f"      • 趋势一致性: {trend_strength.get('trend_consistency', 0):.1f}%")
            
        # 支撑阻力位分析
        print(f"\n   🎯 关键价位分析:")
        sr_levels = self.identify_support_resistance_levels(stock_data)
        current_price = sr_levels.get('current_price', 0)
        print(f"      • 当前价格: {current_price:.2f}元")
        
        resistance = sr_levels.get('immediate_resistance', [])
        if resistance:
            print(f"      • 近期阻力位: {', '.join([f'{r:.2f}' for r in resistance[:2]])}元")
        
        support = sr_levels.get('immediate_support', [])
        if support:
            print(f"      • 近期支撑位: {', '.join([f'{s:.2f}' for s in support[:2]])}元")
            
        # 交易行为特征
        print(f"\n   💹 交易行为特征:")
        trading_behavior = self.analyze_trading_behavior(stock_data)
        
        if trading_behavior:
            print(f"      • 日内波动幅度: {trading_behavior.get('intraday_volatility', 0):.1f}%")
            print(f"      • 跳空次数: {trading_behavior.get('gap_frequency', 0)} 次")
            print(f"      • 成交量波动性: {trading_behavior.get('volume_volatility', 0):.1f}%")
            
        # 风险指标分析
        print(f"\n   ⚠️ 风险指标分析:")
        risk_metrics = self.calculate_risk_metrics(stock_data)
        
        if risk_metrics:
            print(f"      • 年化波动率: {risk_metrics.get('annual_volatility', 0):.1f}%")
            print(f"      • 下行波动率: {risk_metrics.get('downside_volatility', 0):.1f}%")
            print(f"      • VaR(5%): {risk_metrics.get('var_5_percent', 0):.1f}%")
            print(f"      • 最大连续下跌: {risk_metrics.get('max_consecutive_down_days', 0)} 天")
            print(f"      • 夏普比率: {risk_metrics.get('sharpe_ratio', 0):.2f}")
            
        # 投资特征总结
        print(f"\n   💡 投资特征总结:")
        
        # 波动率特征
        volatility = risk_metrics.get('annual_volatility', 20)
        if volatility < 15:
            vol_desc = "低波动，适合稳健投资"
        elif volatility < 25:
            vol_desc = "中等波动，风险适中"
        else:
            vol_desc = "高波动，风险较大"
        print(f"      • 波动特征: {vol_desc}")
        
        # 趋势特征
        trend_consistency = price_patterns.get('trend_strength', {}).get('trend_consistency', 50)
        if trend_consistency > 70:
            trend_desc = "趋势明确，方向性强"
        elif trend_consistency > 40:
            trend_desc = "趋势较为明确"
        else:
            trend_desc = "震荡为主，方向性弱"
        print(f"      • 趋势特征: {trend_desc}")
        
        # 流动性特征
        volume_vol = trading_behavior.get('volume_volatility', 50)
        if volume_vol < 30:
            liquidity_desc = "成交稳定，流动性好"
        elif volume_vol < 60:
            liquidity_desc = "成交一般，流动性中等"
        else:
            liquidity_desc = "成交不稳定，流动性风险"
        print(f"      • 流动性特征: {liquidity_desc}")
        
        # 返回深度分析结果
        result = {
            **stock_info,
            'deep_analysis': {
                'data_points': len(stock_data),
                'price_patterns': price_patterns,
                'sr_levels': sr_levels,
                'trading_behavior': trading_behavior,
                'risk_metrics': risk_metrics,
                'investment_characteristics': {
                    'volatility_desc': vol_desc,
                    'trend_desc': trend_desc,
                    'liquidity_desc': liquidity_desc
                }
            }
        }
        
        return result
        
    def run_deep_analysis(self):
        """运行深度分析系统"""
        self.print_header("第六步：深度分析选中股票 - 价格行为模式识别")
        
        # 加载最终排序结果
        final_ranking = self.load_final_ranking()
        if not final_ranking:
            print("❌ 无法加载最终排序结果，请先执行第五步")
            return []
            
        # 选择Top 3进行深度分析
        top3_stocks = final_ranking[:3]
        print(f"📋 对Top 3股票进行深度分析:")
        for i, stock in enumerate(top3_stocks, 1):
            print(f"   {i}. {stock['code']} {stock['name']} - 综合评分: {stock['comprehensive_score']:.1f}")
            
        deep_analysis_results = []
        
        for i, stock_info in enumerate(top3_stocks, 1):
            print(f"\n[{i}/3] 深度分析进度")
            
            result = self.deep_analyze_single_stock(stock_info)
            if result:
                deep_analysis_results.append(result)
                
        # 生成深度分析报告
        self.generate_deep_analysis_report(deep_analysis_results)
        
        return deep_analysis_results
        
    def generate_deep_analysis_report(self, deep_analysis_results):
        """生成深度分析报告"""
        print(f"\n📊 深度分析总结报告:")
        print(f"   • 深度分析股票数量: {len(deep_analysis_results)}")
        
        if len(deep_analysis_results) > 0:
            print(f"\n🔍 Top 3股票深度对比:")
            print(f"{'股票':<15} {'波动率':<8} {'最大回撤':<8} {'趋势性':<8} {'夏普比率':<8}")
            print("-" * 60)
            
            for result in deep_analysis_results:
                code_name = f"{result['code']} {result['name'][:4]}"
                deep_data = result.get('deep_analysis', {})
                risk_metrics = deep_data.get('risk_metrics', {})
                price_patterns = deep_data.get('price_patterns', {})
                
                volatility = risk_metrics.get('annual_volatility', 0)
                max_dd = price_patterns.get('max_drawdown', 0)
                trend_consistency = price_patterns.get('trend_strength', {}).get('trend_consistency', 0)
                sharpe = risk_metrics.get('sharpe_ratio', 0)
                
                print(f"{code_name:<15} {volatility:<8.1f} {max_dd:<8.1f} {trend_consistency:<8.1f} {sharpe:<8.2f}")
                
            print(f"\n💎 投资价值评估:")
            for i, result in enumerate(deep_analysis_results, 1):
                deep_data = result.get('deep_analysis', {})
                characteristics = deep_data.get('investment_characteristics', {})
                
                print(f"\n   🏅 第{i}名: {result['code']} {result['name']}")
                print(f"      📊 综合评分: {result['comprehensive_score']:.1f}/100")
                print(f"      📈 {characteristics.get('volatility_desc', '')}")
                print(f"      🎯 {characteristics.get('trend_desc', '')}")
                print(f"      💹 {characteristics.get('liquidity_desc', '')}")
                
                # 推荐操作策略
                volatility = deep_data.get('risk_metrics', {}).get('annual_volatility', 20)
                trend_consistency = deep_data.get('price_patterns', {}).get('trend_strength', {}).get('trend_consistency', 50)
                
                if volatility < 20 and trend_consistency > 60:
                    strategy = "适合中长期持有，可逢低加仓"
                elif volatility < 30:
                    strategy = "适合波段操作，注意止盈止损"
                else:
                    strategy = "高风险标的，严格控制仓位"
                    
                print(f"      💡 操作策略: {strategy}")
                
        else:
            print(f"   ⚠️ 没有完成深度分析的股票")
            
    def save_deep_analysis_results(self, deep_analysis_results, filename="deep_analysis_results.csv"):
        """保存深度分析结果"""
        if deep_analysis_results:
            # 简化数据结构用于保存
            save_data = []
            for result in deep_analysis_results:
                deep_data = result.get('deep_analysis', {})
                save_record = {
                    'rank': deep_analysis_results.index(result) + 1,
                    'code': result['code'],
                    'name': result['name'],
                    'comprehensive_score': result['comprehensive_score'],
                    'annual_volatility': deep_data.get('risk_metrics', {}).get('annual_volatility', 0),
                    'max_drawdown': deep_data.get('price_patterns', {}).get('max_drawdown', 0),
                    'trend_consistency': deep_data.get('price_patterns', {}).get('trend_strength', {}).get('trend_consistency', 0),
                    'sharpe_ratio': deep_data.get('risk_metrics', {}).get('sharpe_ratio', 0),
                    'upward_days_ratio': deep_data.get('price_patterns', {}).get('upward_days_ratio', 0),
                    'current_price_position': deep_data.get('price_patterns', {}).get('price_range_52w', {}).get('current_position', 0),
                    'volatility_desc': deep_data.get('investment_characteristics', {}).get('volatility_desc', ''),
                    'trend_desc': deep_data.get('investment_characteristics', {}).get('trend_desc', ''),
                    'liquidity_desc': deep_data.get('investment_characteristics', {}).get('liquidity_desc', '')
                }
                save_data.append(save_record)
                
            df = pd.DataFrame(save_data)
            df.to_csv(filename, index=False, encoding='utf-8-sig')
            print(f"\n💾 深度分析结果已保存到 {filename}")
            return filename
        return None

if __name__ == "__main__":
    # 创建深度分析系统实例
    deep_analyzer = DeepAnalysisSystem()
    
    # 执行深度分析
    results = deep_analyzer.run_deep_analysis()
    
    # 保存结果
    deep_analyzer.save_deep_analysis_results(results)
    
    print(f"\n🎉 第六步：深度分析选中股票执行完成！")
    print(f"   • 成功完成 {len(results)} 只Top股票的深度分析")
    print(f"   • 识别了价格行为模式和投资特征")
    print(f"   • 准备进入第七步：趋势预测系统")
